<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Member extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->template->set_partial('header_search', 'header/header_search');
		$this->template->set_partial('nav_bar', 'header/nav_bar');
		$this->load->spark('Facebook-SDK/0.0.1');
		$this->load->model('member/member_model');
		$this->load->model('member/profile_model');
		$this->load->model('member/sale_model');
		//$this->member_model->check_logedin();
		$this->load->library('parser');
	}

	function index(){
		$this->facebook->destroySession();
		$this->member_model->check_logedin();
		$this->template->set_layout('full_layout');
		$data['what'] = '';



		/*GET PROFILE ID*/


		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}

		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);
		if($isProfile){
			//	$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile']['info'] = $isProfile;
				
				
				
			//$data['profile_id'] = $profile_id;
			//$data['article']['details'] = $this->article_model->getArticle($profile_id);



		}else{
			$data['profile']['info'] = null;
		}


		$data['sale'] = $this->sale_model->getSale($this->session->userdata['mem_id']);
		
		
		
		$this->template->build('dashboard',$data);
	}

	function choose_plan()
	{

		/*GET PROFILE ID*/


		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}

		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);

		if($isProfile){
			//$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile']['info'] = $isProfile;
			//$data['profile_id'] = $profile_id;
			//$data['article']['details'] = $this->article_model->getArticle($profile_id);



		}else{
			$data['profile']['info'] = null;
		}

		$data['plan'] = $this->profile_model->showlist();


		$this->template->set_layout('full_layout');

		$this->template->build('choose_plan',$data);
	}

	function tips_submit()
	{

		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);

		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile_id'] = $profile_id;
			//$data['article']['details'] = $this->article_model->getArticle($profile_id);



		}else{
			$data['profile_id'] = null;
		}

		$this->form_validation->set_rules('helptips', 'helptips', 'trim|xss_clean');
		if ($this->form_validation->run() == true) {
				
			$profile_id = $profile_id;
				
			$post_data = array(
					'profile_id' => $profile_id,
					'title'=> $this->input->post('title'),
					'description' => trim($this->input->post('helptips')),
			);
				
			//var_dump($post_data);
			//die();
				
			$this->db->insert('tips',$post_data);
			$this->session->set_flashdata('message', 'Sucessfully!... ');
			$this->index();
				
		} else {
			$this->session->set_flashdata('message', 'Invalid');
			$this->index();
		}
	}
	function login(){

		$this->load->model('member_model');
		
		$data['fbUrl']= $this->facebook->getLoginURL();
		$user=$this->facebook->getUser();

		if ($user) {
			$user_profile = $this->facebook->api('/me');

			$email = $user_profile['id'];
			$name = $user_profile['name'];
			$identity = 'FB';


			if($this->member_model->checkIdentity($email)== TRUE) {

				//-- Redirect to Dashboard ---//
				$em=$email;
				$sql="select * from member where email='$em'";
				$query = $this->db->query($sql);
				$user = $query->row();

				//print_r($user);
				//die();

				if ($user){

					$this->member_model->clear_session_data("member");

					$this->session->set_userdata('mem_id', $user->id);
					$this->session->set_userdata('mem_email', $user->email);
					$this->session->set_userdata('mem_password', $user->password);
					//$this->session->set_userdata('mem_type', $user->type);
					$this->session->set_userdata('mem_name', $user->name);
					$this->session->set_userdata('mem_last_login_date', $user->last_login_date);
					$this->session->set_userdata('mem_last_login_ip', $user->last_login_ip);
					$this->session->set_userdata('mem_created_on', $user->created_on);
					//-- Update last login of successfull Login
					$sql = "update member set last_login_date = NOW() where email='$user->email'";
					$query = $this->db->query($sql);

					redirect('member');
				}
			}
			else{
				//else store data to member table and redirect to dashboard//--

				$post_data=array(
						'email' =>	$email,
						'name' =>	$user_profile['name'],
						'last_login_ip'=>$this->commoncall->get_ip(),
						'created_on'=> date('Y-m-d H:i:s'),
						'last_login_date'=> date('Y-m-d H:i:s'),
						'identity' => $identity,
						'status' => '51'
				);



				$this->db->insert('member',$post_data);

				if($this->member_model->checkIdentity($email)== TRUE) {

					//-- Redirect to Dashboard ---//
					$em=$email;
					$sql="select * from member where email='$em'";
					$query = $this->db->query($sql);
					$user = $query->row();

					//print_r($user);
					//die();

					if ($user){

						$this->member_model->clear_session_data("member");

						$this->session->set_userdata('mem_id', $user->id);
						$this->session->set_userdata('mem_email', $user->email);
						$this->session->set_userdata('mem_password', $user->password);
						//$this->session->set_userdata('mem_type', $user->type);
						$this->session->set_userdata('mem_name', $user->name);
						$this->session->set_userdata('mem_last_login_date', $user->last_login_date);
						$this->session->set_userdata('mem_last_login_ip', $user->last_login_ip);
						$this->session->set_userdata('mem_created_on', $user->created_on);
						//-- Update last login of successfull Login
						$sql = "update member set last_login_date = NOW() where email='$user->email'";
						$query = $this->db->query($sql);

						redirect('member');
					}
				}

			}

		}


		// 		/$this->template->build('pages/member/login',$data);


		$this->template->set_layout('login_layout');
		$data['module_banner'] = FALSE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = FALSE;


		$this->template->set_breadcrumb('Home', '/')
		//->set_breadcrumb('Category','')
		->set_breadcrumb('Member','member/login')
		->set_breadcrumb('Login','')
		;
		$this->template->build('login',$data);
	}


	function login_submit()
	{

		$this->load->model(array('commoncall','member_model'));

		$this->form_validation->set_rules('login_email', 'Email address', 'trim|required|valid_email');
		$this->form_validation->set_rules('login_password', 'Password', 'trim|required|min_length[6]');

		if ($this->form_validation->run() == true) {
			$this->load->model('member_model');
			$post_data = array(
					'email' => trim($_POST['login_email']),
					'password' => trim($_POST['login_password']));

			if($this->member_model->check_login_submit($post_data)== TRUE){
				redirect('member');
			}else{
				$this->session->set_flashdata('message', "Invalid Login!");
				redirect('member/login');
			}
		}else{
			$this->login();
		}

	}




	function register(){
		$this->template->set_layout('login_layout');
		$data['module_banner'] = FALSE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = FALSE;
		$this->template->set_breadcrumb('Home', '/')
		//->set_breadcrumb('Category','')
		->set_breadcrumb('Member','member/register')
		->set_breadcrumb('Register','')
		;
		$this->template->build('register',$data);
	}

	function submit()
	{
		//$this->form_validation->set_rules('type','Select Type','trim|required|xss_clean');
		$this->form_validation->set_rules('full_name', 'Full Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[member.email]');
		$this->form_validation->set_rules('password','Password','trim|required|xss_clean|min_length[6]');
		$this->form_validation->set_rules('retype_password','Retype Password','trim|required|xss_clean|matches[password]');
		$this->form_validation->set_rules('agree', 'I Agree', 'trim|required');
		$this->form_validation->set_message('is_unique', 'That email address already exists.');

		if ($this->form_validation->run() == true) {
				
			$this->load->model('member_model');
			$post_data = array(
					//'type' => $_POST['type'],
					'name' => $_POST['full_name'],//$this->input->post('full_name'),
					'email' => $_POST['email'],//$this->input->post('textemail'),
					'password' => $_POST['password'],
					'created_on'=> date('Y-m-d H:i:s'),
					'last_login_ip'	=> $this->commoncall->get_ip(),
					'agree'=>$_POST['agree'],
					'status' => 51,
					'activationcode' => md5($_POST['email'].time()),
			);

			if($this->member_model->check_login_submit($post_data)== true){
				$this->login();

			}else{

				$this->db->insert('member',$post_data);

				//--Send Confirmation Email --//
				$email_template = $this->email_template_model->get_template_by_id(2);


				$member_master = $this->member_model->get_member_by_email($this->input->post('email'));

				$member_name = $member_master->name;
				$member_email = $member_master->email;

				$member_password = $member_master->password;
				$website_title = $this->settings_model->get_web_configuration('web_title');
				$website = $this->settings_model->get_web_configuration('web_friendly_name');
				$cutomer_care = $this->settings_model->get_setting('callnow');
				$email_subject = $email_template->title;
				$email_body = $email_template->description;

				$key = $member_master->activationcode;


				$confirm = "<p><a href='".base_url()."member/activationcode/$key' >


				http://lawyersdirectoryscript.com/demo/member/activationcode/$key</a>


				</p>";

				$data = array(
						'member_name' => $member_name,
						'member_email' => $member_email,
						'member_password' => $member_password,
						'confirm' => $confirm,
						'website' => $website,
						'website_title' => $website_title,
						'customer_care_no' => $cutomer_care

				);

				$this->load->library('parser');
				$message = $this->parser->parse_string($email_body, $data, TRUE);
				//$message .= "<p><a href='".base_url()."member/$key' >Click here</a> to confirm your account</p>";



				//var_dump($confmessage);
				//die();

				$this->settings_model->send_mail($member_email, $email_subject, $message);




				$this->session->set_flashdata('message', "Thank You for resgister! Please Check Your Email...");
				//$message .= "<p><a href='".base_url()."index.php/HomeController/register_user/$key' >Click here</a> to confirm your account</p>";

				redirect('member/register');
			}
		}else{

			$this->register();
		}

	}



	function profile_image(){


		/*GET PROFILE ID*/
		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);

		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile']['info'] = $isProfile;
			$data['profile_id'] = $profile_id;

				
			$sql="select photo from profile where id=$profile_id";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0) {
				$row = $query->row();
					
			}
				
			//$data['profile'] = $member_id;
				
				
			$data['photo']=$row->photo;

		}else{
			$data['profile']['info'] = null;
		}
		/*GET PROFILE ID*/
		//$data['member_id'] = $member_id;

		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}





		$this->template->set_layout('full_layout');
		$this->template->build('profile_image',$data);
	}


	function upload_profile_image()
	{

		/*GET PROFILE ID*/
		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);

		if($isProfile){
			//$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile']['info'] = $isProfile;
			//$data['profile_id'] = $profile_id;


		}else{
			$data['profile']['info'] = null;
		}
		/*GET PROFILE ID*/
		//$data['member_id'] = $member_id;

		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}


		if ($this->input->post('upload')) {

			$profile_id = $_POST['profile_id'];

			$filevariable=array_keys($_FILES);
			$userfile=$filevariable[0];
			$image=$_FILES[$userfile];

			//$vSize = $this->input->post('banner_size');
			//$vSize = explode('X',$vSize);
			//$vH=0;
			//$vW=0;

			$vW='146';
			$vH='191';


			$imageData=array(
					'userfile'=>$userfile,
					'imagearray'=>$image,
					'imagepath'=>'./assets/images/photo/',
					'allowedtype'=>'jpg|png|gif|jpeg',
					'overwrite'=>'true',
					'removespace'=>'true',
					'customefilename'=>'',     		//do not use extension if not need leave blank
					'namewithtime'=>'yes', 			//time(image_121326.jpg)at last of file name if not need leave blank
					'separator'=>'-', 				//format - image_121326.jpg (.)not allowed, if not require leave blank
					'prefix'=>'tutor_',				//format my_image_121326.jpg , if not require leave blank
					'errmessage'=>'',               // If blank default message will shown
					'sucessmessage'=>'',            // If blank default message will shown
					'createthumb'=>'yes',
					'thumbwidth'=>$vW,
					'thumbheight'=>$vH,
					'thumbpath'=>'' 				// Dont write thumb folder name in path

			);

			$uploadData=$this->settings_model->upload($imageData);

			//print_r($uploadData);
			//die();


			$id = $this->input->post('profile_id');

			$data=array(

					//'id'=>$listing_id,
					'photo'=>$uploadData['data'],

			);

			if ($uploadData['data']  == TRUE ) {


				$image = $this->member_model->lawyer_image($profile_id);

				if ($image->photo != 'no_profile_image.png') {
					$f = BASEPATH.'../assets/images/photo/' . $image->photo;
					unlink($f);
				}

				/*$sql = "Select logo from business_master where id= $id";
				 $filename = $this->db->query($sql)->row()->logo;
				$f = BASEPATH.'../assets/images/data/logo/'.$filename;
				unlink($f);
				*/

				$this->db->where('id', $id);
				$this->db->update('profile',$data);
				$this->session->set_flashdata('message', 'Image Uploded Sucessfully!... ');
				redirect('member/profile_image/');
					
			} else {
					
				$this->session->set_flashdata('message', 'Fields can not be null!... ');
				redirect('member/profile_image/');
				exit();

			}
		} else {

			$this->session->set_flashdata('message', 'Please Choose a correct file!... ');
			redirect('member/profile_image/');
			exit();

		}
	}


	function post_mail()
	{

		$this->form_validation->set_rules('contactname', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('contactemail', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('location', 'Location', 'trim|required|xss_clean');
		$this->form_validation->set_rules('purpose', 'Purpose', 'trim|required|xss_clean');
		$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean|min_length[50]');

		if ($this->form_validation->run() == false) {
			$message = "Invalied input!";
		}
		else{


			$email_template = $this->email_template_model->get_template_by_id(11);
			$default_mail = $this->settings_model->get_web_configuration('default_email');
			$website_title = $this->settings_model->get_web_configuration('web_title');

			$email_subject = $email_template->title;
			$email_body = $email_template->description;



			$data = array(
					//'prefix'	=> 'ENQ',
					//'listing_id' => $this->input->post('listing_id'),
					'wesite_title' => $website_title,
					'name' => $this->input->post('contactname'),
					'email' => $this->input->post('contactemail'),
					'location' => $this->input->post('location'),
					'purpose' => $this->input->post('purpose'),
					'description' => $this->input->post('description'),

					//'ip' => $this->commoncall->get_ip(),

			);


			$message = $this->parser->parse_string($email_body, $data, TRUE);
			$this->commoncall->send_mail($default_mail, $email_subject, $message);

			$message = "Email Submitted....";
		}
			
		$output = '{ "message": "'.$message.'"}';
		echo $output;
			
	}





	function show_tips()
	{
		$data['mode'] = "SHOW";

		$data['tips'] = $this->member_model->showTips();

		$this->load->view('tips_view',$data);
	}

	function tipsview(){

		$data['mode'] = "VIEW";

		$data['site_title'] = $this->settings_model->siteInfo('web_title');
		$data['keyword'] = $this->settings_model->siteInfo('keyword');
		$data['description'] = $this->settings_model->siteInfo('description');

		$tips_id = $this->commoncall->get_id(current_url());

		$data['tips']['info'] = $this->member_model->getTipsinfo($tips_id);


		$this->template->set_breadcrumb('Home', '/')
		->set_breadcrumb('Help and Tips', '');

		$this->template->title('Help and Tips', $data['site_title']);
		$this->template->set_metadata('keywords', $data['keyword']);
		$this->template->set_metadata('description', $data['description']);

		$this->template->set_layout('details_layout');
		$this->template->set_partial('header_search', 'header/header_search');

		$this->template->build('tips_view',$data);

	}


	function delete($id)
	{
		$this->member_model->delete($id);
		$this->index();
	}
	function activationcode($link){
		//die($link);
		$sql = "update member set status=51 where activationcode='$link'";
		$this->db->query($sql);
		redirect('member/login');
	}

	function view_error()
	{

		$this->template->set_layout('error_layout');

		$this->template->build('news/support');
	}


	function forgot_password()
	{
		$this->template->set_breadcrumb('Home', '/')
		//->set_breadcrumb('Category','')
		->set_breadcrumb('Member','member/login')
		->set_breadcrumb('Forgot Password','');
		$this->template->set_layout('login_layout');
		$this->template->build('forgot_password');
	}

	function submit_forgot_password()
	{
		$status = '';
		$email = $this->input->post('login_email');
		$this->form_validation->set_rules('login_email', 'Enter Your Email ID', 'trim|required|xss_clean|valid_email');
		if ($this->form_validation->run()) {
			$email_template = $this->email_template_model->get_template_by_id(5);
			$website_title = $this->settings_model->get_web_configuration('web_title');
			$website = $this->settings_model->get_web_configuration('web_friendly_name');
			$cutomer_care = $this->settings_model->get_setting('callnow');
			$member_master = $this->member_model->get_member_by_email($email);
			$email_subject = $email_template->title;
			$email_body = $email_template->description;
			if ($member_master) {
				$member_email = $member_master->email;
				$emaildata = array(
						'member' => $member_master->name,
						'userid' => $member_master->email,
						'password' => $member_master->password,
						'website' => $website,
						'website_title' => $website_title,
						'customer_care_no' => $cutomer_care
				);
				//print_r($emaildata);
				$this->load->library('parser');
				$message = $this->parser->parse_string($email_body, $emaildata, TRUE);
				$this->commoncall->send_mail($member_email, $email_subject, $message);
				$this->session->set_flashdata('msg', "Password has sent, check your mail..");

				redirect('member/forgot_password');
			} else {
				$this->session->set_flashdata('msg', "Invalid Operation...");
				redirect('member/forgot_password');
			}
		}
		$this->session->set_flashdata('msg', "Enter Valid Email..");
		redirect('member/forgot_password');

	}

}
/* End of file*/
